<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Gregwar\Captcha\CaptchaBuilder;

class LoginController extends Controller
{
	//加载登录模板
   public function login()
   {
	   return view('admin.login');
   }
   
   //执行用户登录
   public function doLogin(Request $request)
   {
	   $mycode = $request->input("mycode");
	   $yanzhengma = $request->session()->get('phrase');
	   if($mycode !== $yanzhengma){
		   return back()->with('msg',"验证码错误！".$mycode.":".$yanzhengma);
	   }
	   //执行登录判断
	   $name = $request->input('name');
	   $password = $request->input('password');
	   
	   //获取对应用户信息
	   $user = \DB::table('users')->where("name",$name)->first();
	   
		// dd($user);
	   //判断用户是否存在
	   if(!empty($user)){
		   //判断用户的权限
		   if($user->status == 1){
			   //判断密码是否正确
			   if(md5($password) == $user->password){
			   	
				   //存储session，跳转页面
				   session()->set("adminuser",$user);
				   //获取当前登陆者的权限
					$res = \DB::select('select n.name,n.method,n.url  from user_role ur,role_node rn,node n where ur.rid=rn.rid and rn.nid=n.id and ur.uid=:id', ['id' => $user->id]);
					$nodelist = array(array("name"=>"网站首页","method"=>"get","url"=>"admin"));
					foreach($res as $ob){
						$nodelist[] = [
							   "name"=>$ob->name,
							   "method"=>$ob->method,
							   "url"=>$ob->url
							];
						//判断当前是否有添加权限,若有就追加执行添加
						if(preg_match("/^.*?\/create$/",$ob->url) && $ob->method=="get"){
							$nodelist[] =[
							   "name"=>"执行添加",
							   "method"=>"post",
							   "url"=>preg_replace("/^(.*?)\/create$/","\\1",$ob->url)
							];
						}
						//判断当前是否有修改权限,若有就追加执行修改
						if(preg_match("/^.*?\/\*\/edit$/",$ob->url) && $ob->method=="get"){
							$nodelist[] =[
							   "name"=>"执行修改",
							   "method"=>"put",
							   "url"=>preg_replace("/^(.*?)\/\*\/edit$/","\\1/*",$ob->url)
							];
						}
					}
					//将获取的权限放入到session中
					session()->set("nodelist",$nodelist);
			
				   return redirect("admin");
			   }
			   return back()->with("msg","密码不正确！");

		   }
		   
		   return back()->with('msg',"用户无权限！$user->status");
	   }
	   return back()->with('msg',"用户不存在！");
   }		
	   
   
   
   //加载验证码
   public function getCode()
   {
	   $builder = new CaptchaBuilder();
	   $builder->build(150,32);
	   \Session::set('phrase',$builder->getPhrase()); //存储验证码
	   return response($builder->output())->header('Content-type','image/jpeg');
   }
   
   //执行退出
   public function logout(Request $request)
   {
	   $request->session()->forget('adminuser'); //从session中移除数据
	   
	   return redirect("admin/login");
   }
   
}
